package com.castle.fortress.admin.temp.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.temp.entity.TempTreeTableEntity;
import com.castle.fortress.admin.temp.dto.TempTreeTableDto;
import com.castle.fortress.admin.system.entity.SysUser;
import com.castle.fortress.admin.temp.service.TempTreeTableService;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.admin.core.annotation.CastleLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 生成左树右表示例 api 控制器
 *
 * @author 
 * @since 2023-08-02
 */
@Api(tags="生成左树右表示例api管理控制器")
@RestController
@RequestMapping("/api/temp/tempTreeTable")
public class ApiTempTreeTableController {
	@Autowired
	private TempTreeTableService tempTreeTableService;


	/**
	 * 生成左树右表示例的分页展示
	 * @param tempTreeTableDto 生成左树右表示例实体类
	 * @param currentPage 当前页
	 * @param size  每页记录数
	 * @return
	 */
    @CastleLog(operLocation="生成左树右表示例",operType = OperationTypeEnum.QUERY)
	@ApiOperation("生成左树右表示例分页展示")
	@GetMapping("/page")
	public RespBody<IPage<TempTreeTableDto>> pageTempTreeTable(TempTreeTableDto tempTreeTableDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
		Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
		Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
		Page<TempTreeTableDto> page = new Page(pageIndex, pageSize);

		IPage<TempTreeTableDto> pages = tempTreeTableService.pageTempTreeTable(page, tempTreeTableDto);
		return RespBody.data(pages);
	}

	/**
	 * 生成左树右表示例保存
	 * @param tempTreeTableDto 生成左树右表示例实体类
	 * @return
	 */
    @CastleLog(operLocation="生成左树右表示例",operType = OperationTypeEnum.INSERT)
	@ApiOperation("生成左树右表示例保存")
	@PostMapping("/save")
	public RespBody<String> saveTempTreeTable(@RequestBody TempTreeTableDto tempTreeTableDto){
		if(tempTreeTableDto == null ){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
			TempTreeTableEntity tempTreeTableEntity = ConvertUtil.transformObj(tempTreeTableDto,TempTreeTableEntity.class);
		if(tempTreeTableService.save(tempTreeTableEntity)){
			return RespBody.data("保存成功");
		}else{
			return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
		}
	}

	/**
	 * 生成左树右表示例编辑
	 * @param tempTreeTableDto 生成左树右表示例实体类
	 * @return
	 */
    @CastleLog(operLocation="生成左树右表示例",operType = OperationTypeEnum.UPDATE)
	@ApiOperation("生成左树右表示例编辑")
	@PostMapping("/edit")
	public RespBody<String> updateTempTreeTable(@RequestBody TempTreeTableDto tempTreeTableDto){
		if(tempTreeTableDto == null || tempTreeTableDto.getId() == null || tempTreeTableDto.getId().equals(0L)){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
			TempTreeTableEntity tempTreeTableEntity = ConvertUtil.transformObj(tempTreeTableDto,TempTreeTableEntity.class);
		if(tempTreeTableService.updateById(tempTreeTableEntity)){
			return RespBody.data("保存成功");
		}else{
			return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
		}
	}

	/**
	 * 生成左树右表示例删除
	 * @param ids 生成左树右表示例id集合
	 * @return
	 */
    @CastleLog(operLocation="生成左树右表示例",operType = OperationTypeEnum.DELETE)
	@ApiOperation("生成左树右表示例删除")
	@PostMapping("/delete")
	public RespBody<String> deleteTempTreeTable(@RequestBody List<Long> ids){
		if(ids == null || ids.isEmpty()){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
		if(tempTreeTableService.removeByIds(ids)) {
			return RespBody.data("保存成功");
		}else{
			return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
		}
	}

	/**
	 * 生成左树右表示例详情
	 * @param id 生成左树右表示例id
	 * @return
	 */
    @CastleLog(operLocation="生成左树右表示例",operType = OperationTypeEnum.QUERY)
	@ApiOperation("生成左树右表示例详情")
	@GetMapping("/info")
	public RespBody<TempTreeTableDto> infoTempTreeTable(@RequestParam Long id){
		if(id == null){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
		TempTreeTableDto tempTreeTableDto =  tempTreeTableService.getByIdExtends(id);
		return RespBody.data(tempTreeTableDto);
	}


}
